package com.example.demo03_mvm.db;

import androidx.annotation.NonNull;
import androidx.room.Room;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.example.demo03_mvm.App;

public class DBInstance {

    public static final String DB_NAME="mygoods";
    public static GoodsDataBase goodsDataBase;

    public static final Migration MIGRATION=new Migration(2,3){

        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //database.execSQL("create table kf(id integer primary key autoincrement,msg varchar(50),who int not null)");
            database.execSQL("alter table kf add gid int not null default 1");
        }
    };

    public static GoodsDataBase getGoodsDataBase(){

        if(goodsDataBase==null){
            synchronized (DBInstance.class){
                if(goodsDataBase==null){
                    goodsDataBase= Room.databaseBuilder(App.getContext(),GoodsDataBase.class,DB_NAME)
                            .addMigrations(MIGRATION)
                            //.allowMainThreadQueries()
                            .build();
                }
            }
        }
        return goodsDataBase;
    }
}
